Skip to content

#8 Creating database and user & granting user privileges to database#2

Merged
songz merged 12 commits into
garageScript:masterfrom
allopez7:postgres
Apr 6, 2020
Merged

#8 Creating database and user & granting user privileges to database#2
songz merged 12 commits into
garageScript:masterfrom
allopez7:postgres

Conversation

@allopez7
Copy link
Copy Markdown

@allopez7 allopez7 commented Mar 9, 2020

No description provided.

@allopez7 allopez7 changed the title Postgres Creating database and user & granting user privileges to database Mar 10, 2020
Comment thread database.js Outdated
});

client.connect().then(() => {
createPgAccount("username", "password", "database");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this doing.

Copy link
Copy Markdown
Author

@allopez7 allopez7 Mar 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

calling the function createPgAccount and passing in dummy data as arguments for now. Eventually, data from the sign-up page will get passed in.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove, or else this script will throw error.

Comment thread database.js Outdated
Comment thread database.js Outdated
Comment thread database.js Outdated

const userDatabasePrivileges = await client.query(`GRANT ALL PRIVILEGES ON DATABASE ${database} To ${username}`)

await client.end()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is your client ending when you've created 1 account? By doing this, you can't run this function more than once.

Comment thread database.js Outdated

const createPgAccount = async (username, password, database) => {
try{
const createDatabase = await client.query(`CREATE DATABASE ${database}`)
Copy link
Copy Markdown

@hwong0305 hwong0305 Mar 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to assign the return the value to variables when it's not being used?

It should just be await client.query(CREATE DATABASE ${database})

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point

Comment thread database.js Outdated

client.connect().then(() => {
console.log('connected')
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put these inside a startPgDB function.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comment

// when the app starts, we should first start all the databases we support (ie PostGres, MongoDb, Neo4J). If the DB can't start up, then our service is useless, so don't even start the app.

@allopez7 allopez7 added the Databases Postgres, mongoDB, neo4j label Mar 25, 2020
@allopez7 allopez7 self-assigned this Mar 25, 2020
Comment thread package.json Outdated
},
"scripts": {
"test": "jest",
"start": "node database.js"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

node index.js <- usually the default

Comment thread database/postgres/pg.js Outdated
Comment thread database/postgres/pg.js Outdated
try{
await client.end()
}catch(err){
console.log('connection failed to closed', err)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should return an error so whomever calls closePGDB will know that it has failed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add this as a test case

Comment thread database/postgres/pg.js
Comment thread database/postgres/pg.js Outdated
Comment thread database/postgres/pg.js
Comment thread database/postgres/pg.js
Comment thread database/postgres/pg.test.js Outdated
describe('Test startPGDB && closePGDB', ()=>{
it('startPGDB', async ()=>{
const startRes = await startPGDB()
if(startRes && startRes.error && startRes.error.message){
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove if statement.

Comment thread database/postgres/pg.js
Comment thread database/postgres/pg.test.js Outdated
Comment thread database/postgres/pg.test.js Outdated
expect(mockClient.query).toHaveBeenNthCalledWith(3, `GRANT ALL PRIVILEGES ON DATABASE username TO username`)
expect(async ()=>{
await createPgAccount()
}).not.toThrow()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expecting it to not throw is not doing anything. because your test doesn't have try catch. If the function threw something, it would not pass

Comment thread database/postgres/pg.test.js Outdated
expect(mockClient.query).toHaveBeenNthCalledWith(2, `DROP USER IF EXISTS username`)
expect(async ()=>{
await deletePgAccount('username')
}).not.toThrow()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expecting it to not throw is not doing anything. because your test doesn't have try catch. If the function threw something, it would not pass

Comment thread database/postgres/pg.test.js Outdated
})
})
describe('Test createPgAccount', ()=>{
it('createPgAccount', async ()=>{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should run all necessary queries in a successful createPgAccount call

Comment thread database/postgres/pg.test.js Outdated
expect(mockClient.query).toHaveBeenNthCalledWith(3, `GRANT ALL PRIVILEGES ON DATABASE username TO username`)
await closePGDB()
})
it('createPgAccount, undefined parameters', async ()=>{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should not execute any queries if required arguments are not passed in.

Comment thread database/postgres/pg.test.js Outdated
expect(mockClient.query).toHaveBeenNthCalledWith(2, `DROP USER IF EXISTS username`)
await closePGDB()
})
it('deletePgAccount, undefined parameters', async ()=>{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should not execute any queries in deletePgAccount if required arguments are not passed in.

Comment thread database/postgres/pg.test.js Outdated
})
})
describe('Test deletePgAccount', ()=>{
it('deletePgAccount', async ()=>{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should execute all queries if required arguments are passed into deletePgAccount

Comment thread database/postgres/pg.test.js Outdated
}
let {startPGDB, closePGDB, createPgAccount, deletePgAccount} = require('./pg')
describe('Test startPGDB && closePGDB', ()=>{
it('startPGDB', async ()=>{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should call connect when starting PGDB

Comment thread database/postgres/pg.test.js Outdated
await startPGDB()
expect(mockClient.connect).toHaveBeenCalledTimes(1)
})
it('closePGDB', async ()=>{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should call end when starting PGDB

Comment thread database/postgres/pg.test.js Outdated
@@ -0,0 +1,61 @@
const pgClient = require('pg')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jest.mock('pg')
const {Client} = require('pg')
const {startPGDB.....} = require('./pg')

Comment thread database/postgres/pg.test.js Outdated
this.query = mockClient.query
this.connect = mockClient.connect
this.end = mockClient.end
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in addition to comment (above), you can do this to be more concise:

Client.mockImplementation(function() {
  return mockClient
})

Comment thread database/postgres/pg.test.js Outdated
const {startPGDB, closePGDB, createPgAccount, deletePgAccount} = require('./pg')

describe('Test PG DB', ()=>{
afterEach(()=>{
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be beforeEach(...) shoudn't it?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be beforeEach as well but on the first test the mocks would be cleared when there is no need to clear.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be beforeEach. As the test suite gets more complex, this will cause issues later on, you can't rely on other ppl to always clear their test.

Comment thread database/postgres/pg.test.js Outdated
Client.mockImplementation(function(){
return mockClient
})
global.console.log = jest.fn()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's console.log = jest.fn(), also it'll be clearer if it moves to under 47th line

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but that wouldn't make sense for line 75 where it's called again. Since it's global i'm changing the functionality of console so I wanted to make it clear that this is changing the functionality of console for the whole database file. You do you have a good point so I'm going to take it outside of the postgres test so we can use it in the MongoDB and Neo4J test as well.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be scoped into the function itself. Its not good for a developer later on to try to console.log something, but nothing shows up. This will essentially remove console.log functionality, which will remove debugging feature for alot of devs. Better to be inside the test.

Comment thread database/postgres/pg.test.js
@songz songz merged commit 039de07 into garageScript:master Apr 6, 2020
@allopez7 allopez7 linked an issue Apr 6, 2020 that may be closed by this pull request
@allopez7 allopez7 removed a link to an issue Apr 6, 2020
@allopez7 allopez7 linked an issue Apr 6, 2020 that may be closed by this pull request
@allopez7 allopez7 removed a link to an issue Apr 6, 2020
@allopez7 allopez7 linked an issue Apr 6, 2020 that may be closed by this pull request
@allopez7 allopez7 changed the title Creating database and user & granting user privileges to database #8 Creating database and user & granting user privileges to database Apr 6, 2020
@allopez7 allopez7 removed a link to an issue Apr 6, 2020
@allopez7 allopez7 linked an issue Apr 6, 2020 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Databases Postgres, mongoDB, neo4j

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement PG DB Module

7 participants